package dobest.ray.com.jiongjiong.activity.HomePage;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.activity.BaseFragment;
import dobest.ray.com.jiongjiong.activity.MainActivity;
import dobest.ray.com.jiongjiong.adapter.HomePageFragmentPagerAdapter;
import dobest.ray.com.jiongjiong.bean.PresenterData;
import dobest.ray.com.jiongjiong.callback.PresenterCallBack;


/**
 *
 *
 * @author wangl01
 */
public class HomePageFragment extends BaseFragment implements OnClickListener, PresenterCallBack {
    /**
     * activity 实例
     */
    private Activity mActivity;
    private MainActivity mainActivity;
    private Resources resources;

    private TextView tv_home_page_vison_status;
    private TextView tv_home_page_vison_trede;
    private ViewPager mPager;
    private ArrayList<Fragment> fragmentsList;
    private View view_trend;
    private View view_status;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mActivity = getActivity();
        mainActivity = (MainActivity) mActivity;
        return inflater.inflate(R.layout.fragment_home_page, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    protected void findViews(View view) {
        resources = getResources();

        view_trend = view.findViewById(R.id.view_trend);
        view_status = view.findViewById(R.id.view_status);

        InitTextView(view);
        InitViewPager(view);
    }

    protected void init() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    protected void addListeners() {
    }

    @SuppressWarnings("unused")
    @Override
    public void onClick(View view) {
        Intent it = null;
        switch (view.getId()) {


            default:
                break;
        }
        if (it != null) {
            mActivity.startActivity(it);
        }

    }

    @Override
    public <T> void callBackPresenter(PresenterData<T> data) {

    }


    private void InitTextView(View view) {
        tv_home_page_vison_status = (TextView) view.findViewById(R.id.tv_home_page_vison_status);
        tv_home_page_vison_trede = (TextView) view.findViewById(R.id.tv_home_page_vison_trede);

        tv_home_page_vison_status.setOnClickListener(new MyOnClickListener(0));
        tv_home_page_vison_trede.setOnClickListener(new MyOnClickListener(1));
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
            switch (index) {
                case 0:
                    view_status.setVisibility(View.VISIBLE);
                    view_trend.setVisibility(View.INVISIBLE);

                    tv_home_page_vison_trede.setTextAppearance(mActivity, R.style.TextNormal);
                    tv_home_page_vison_status.setTextAppearance(mActivity, R.style.TextBold);
                    break;
                case 1:
                    view_status.setVisibility(View.INVISIBLE);
                    view_trend.setVisibility(View.VISIBLE);
                    tv_home_page_vison_trede.setTextAppearance(mActivity, R.style.TextBold);
                    tv_home_page_vison_status.setTextAppearance(mActivity, R.style.TextNormal);
                    break;
            }
        }
    }


    private void InitViewPager(View view) {
        mPager = (ViewPager) view.findViewById(R.id.vPager);
        mPager.setOffscreenPageLimit(2);
        fragmentsList = new ArrayList<Fragment>();

        Fragment visionStatueFragment = new VisionStatueFragment();
        Fragment visionTrendFragment = new VisionTrendFragment();

        fragmentsList.add(visionStatueFragment);
        fragmentsList.add(visionTrendFragment);

        mPager.setAdapter(new HomePageFragmentPagerAdapter(mainActivity.getSupportFragmentManager(), fragmentsList));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:
                    view_status.setVisibility(View.VISIBLE);
                    view_trend.setVisibility(View.INVISIBLE);

                    tv_home_page_vison_trede.setTextAppearance(mActivity, R.style.TextNormal);
                    tv_home_page_vison_status.setTextAppearance(mActivity, R.style.TextBold);
                    break;
                case 1:
                    view_status.setVisibility(View.INVISIBLE);
                    view_trend.setVisibility(View.VISIBLE);
                    tv_home_page_vison_trede.setTextAppearance(mActivity, R.style.TextBold);
                    tv_home_page_vison_status.setTextAppearance(mActivity, R.style.TextNormal);
                    break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }
}
